import Mock from "mockjs";

// 定义的数据
// var data = {};

// // 条件判断生成数据
// if (localStorage.getItem("user_info")) {
//     data = JSON.parse(localStorage.getItem("user_info"));
// } else {
//     // 生成数据
//     data = Mock.mock({
//         "list|50": [{
//             'phone|13000000000-19000000000': 1,
//             'password|100000-1000000000': 1
//         }]
//     })
//     localStorage.setItem("user_info", JSON.stringify(data));
// }


// 库存货物量模拟
var goods_amount = {};
var img = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fupload.shejihz.com%2F2020%2F05%2Fbf1672b57c214af392210c795e868ddc.jpg&refer=http%3A%2F%2Fupload.shejihz.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1669866262&t=59cc712e302ad5058cc948af8e4f006c'
    // 判断本地存储是否含有数据
if (localStorage.getItem('user_amount')) {
    goods_amount = JSON.parse(localStorage.getItem('user_amount'))
} else {
    // 生成数据
    goods_amount = Mock.mock({
            "list|50": [{
                goodsImg: img,
                "goodsid|1000-9999": 1,
                goodsname: "@pick(['连衣裙','卫衣','秋季外套','男士上衣','羽绒服','皮夹克','飞行夹克','冲锋衣','棉服','马甲','短袖','长袖T恤','睡衣','背心','太空棉服'])",
                "goodspreon|9-999": 1,
                "goodsamount|1-88": 1,
                maxamount: 200,
                replenish: 0,

            }]
        })
        // 生成数据后要往本地存储中存一份数据
    localStorage.setItem('user_amount', JSON.stringify(goods_amount));
}




// 查询-所有 商品库存量 接口 1
Mock.mock('/api/user/amount', 'post', (req) => {
    return { status: 200, msg: '查询成功', list: goods_amount.list }
}) /*  */

// 删除一个商品数据 接口 2 
Mock.mock('/api/user/modelete', 'post', (req) => {
        //接收参数 , req.body 是字符串格式,需要转为json对象
        var { id } = JSON.parse(req.body);

        // 通过id，找到要删除的那一条数据
        var i = goods_amount.list.findIndex((item) => {

            return item.goodsid == id;

        })
        console.log(i)
        console.log(id);
        if (i != -1) {
            goods_amount.list.splice(i, 1);
            // console.log(goods_amount);
            localStorage.setItem('user_amount', JSON.stringify(goods_amount));
            return { status: 200, msg: '删除成功!', list: goods_amount.list, }
        }
        return { status: 400, msg: '删除失败!', list: goods_amount.list }
    })
    // 编辑修改商品库存信息 接口 3
Mock.mock('/api/user/amend', 'post', (req) => {
        var { obj } = JSON.parse(req.body);
        return JSON.parse(req.body);
    })
    // 真———修改商品库存信息
Mock.mock('/api/user/amends', 'post', (req) => {
    var { obj } = JSON.parse(req.body);
    var id = obj.goodsid;
    // console.log(obj);

    var i = goods_amount.list.findIndex((item) => {

            return item.goodsid == id;

        })
        // console.log(i)
    if (i != -1) {
        goods_amount.list[i].goodsname = obj.goodsname;
        goods_amount.list[i].goodsamount = obj.goodsamount;
        goods_amount.list[i].maxamount = obj.maxamount;
        localStorage.setItem('user_amount', JSON.stringify(goods_amount));
        return { status: 200, msg: '编辑成功!', list: goods_amount.list, }
    } else {
        return { status: 400, msg: '编辑失败!', list: goods_amount.list, }
    }
})

// 查询某个商品数据 接口 4
Mock.mock('/api/user/mosearch', 'post', (req) => {
    //接收参数 , req.body 是字符串格式,需要转为json对象
    var { id } = JSON.parse(req.body);
    // console.log(id);

    var i = goods_amount.list.findIndex((item) => {
        return item.goodsid == id;
    });

    var goodsdata = { list: [] };

    if (i != -1) {
        goodsdata.list.push(goods_amount.list[i]);
        // console.log(goodsdata.list);
        return { states: 200, usg: '查询成功', list: goodsdata.list }
    } else {
        return { states: 404, usg: '查询失败', list: goodsdata.list }
    }

});

// 新增商品数据 接口 5
Mock.mock('/api/user/addgoods', 'post', (req) => {
        var { obj } = JSON.parse(req.body);
        // console.log(obj);
        obj.goodsImg = '';
        obj.replenish = 0;
        // console.log(obj)
        goods_amount.list.unshift(obj);
        localStorage.setItem('user_amount', JSON.stringify(goods_amount));
        return { status: 200, msg: '添加成功!', list: goods_amount.list, }
    })
    // 急需补货提示页面接口
Mock.mock('/api/user/hint', 'post', (req) => {
    var arr = [];
    goods_amount.list.forEach(item => {
        if (item.goodsamount < 20) {
            arr.push(item)
        }
    });
    return { status: 200, msg: '查询成功!', list: arr };
})